package com.example.demo.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.example.demo.entity.Employee;


@Controller
public class LoginController {
	
	@GetMapping("/indexa")
	public String index() {
		return "index";
	}
	
	@PostMapping("/doindex")
	public String doindexsdfsdgf(String uname,String upwd,Model model) {
		//判断uname==admin 并且  upwd==123456 如果满足条件，跳转到success，否则跳转到index
		//通过控制器跳转到add.html (form) uname,pwd,gender,address,birthday, 点击提交，控制器中接收打印出来，
		//跳转到success
		//springmvc:企业级框架，servlet框架，前端有数据，
		//首先把这个数据传递到控制器（controller,sprigmvc）
		//从后台数据库拿到了一个对象，想展示到前端，也是通过controller
		if("admin".equals(uname)&&"123456".equals(upwd)) {
			model.addAttribute("username123", uname);
			return "success";
		}
		//如果失败，传递一个String类型的变量msg,在index页面显示“登陆失败”
		model.addAttribute("msg", "登陆失败");
		return "index";
		
	}
	
	@GetMapping("/toSearch")
	public String toSearch(Model model) throws ParseException {
		
		Employee emp=new Employee();
		emp.setEmpid(101);
		emp.setEmpname("jack");
		emp.setSalary(8000);
		emp.setDeptid(60);
		Date date=new SimpleDateFormat("yyyy-MM-dd").parse("2000-01-01");
		emp.setBirthday(date);
		
		model.addAttribute("emp", emp);
		
		return "main";
	}
	@GetMapping("/toSearchs")
	public String toSearchs(Model model) throws ParseException {
		
		Employee emp=new Employee();
		emp.setEmpid(101);
		emp.setEmpname("jack");
		emp.setSalary(8000);
		emp.setDeptid(60);
		Date date=new SimpleDateFormat("yyyy-MM-dd").parse("2000-01-01");
		emp.setBirthday(date);
		
		Employee emp1=new Employee();
		emp1.setEmpid(102);
		emp1.setEmpname("rose");
		emp1.setSalary(8000);
		emp1.setDeptid(60);
		Date date1=new SimpleDateFormat("yyyy-MM-dd").parse("2001-01-01");
		emp1.setBirthday(date1);
		
		Employee emp2=new Employee();
		emp2.setEmpid(103);
		emp2.setEmpname("seven");
		emp2.setSalary(9000);
		emp2.setDeptid(60);
		Date date2=new SimpleDateFormat("yyyy-MM-dd").parse("2002-01-01");
		emp2.setBirthday(date2);
		
		List<Employee> emps=new ArrayList<Employee>();
		emps.add(emp);
		emps.add(emp1);
		emps.add(emp2);
		
		model.addAttribute("emps", emps);
		return "main";
	}


}
